/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.prepay.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.modules.base.entity.fixedexpense.FixedExpense;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.entity.taccount.Taccount;
import com.jeesite.modules.base.service.fixedexpense.FixedExpenseService;
import com.jeesite.modules.base.service.supplier.SupplierService;
import com.jeesite.modules.base.service.taccount.TaccountService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.financing.service.finserviceorder.YYService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.prepay.entity.TprepaymentActual;
import com.jeesite.modules.prepay.entity.TprepaymentEstimated;
import com.jeesite.modules.prerecieve.entity.TadvancesReceived;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.prepay.entity.Tprepayment;
import com.jeesite.modules.prepay.service.TprepaymentService;

import java.util.ArrayList;
import java.util.List;

/**
 * 预付款Controller
 * @author wsm
 * @version 2021-03-18
 */
@Controller
@RequestMapping(value = "${adminPath}/prepay/tprepayment")
public class TprepaymentController extends BaseController {

	@Autowired
	private TprepaymentService tprepaymentService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private ServiceOrderService serviceOrderService;
	@Autowired
	private FixedExpenseService fixedExpenseService;
	@Autowired
	private YYService yyService;
	@Autowired
	private TaccountService taccountService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Tprepayment get(String prepaymentId, boolean isNewRecord) {
		return tprepaymentService.get(prepaymentId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = {"list", ""})
	public String list(Tprepayment tprepayment, Model model) {
		//供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList",supplierList);
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList",serviceOrderList);
		model.addAttribute("tprepayment", tprepayment);
		return "modules/prepay/tprepaymentList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Tprepayment> listData(Tprepayment tprepayment, HttpServletRequest request, HttpServletResponse response) {
		tprepayment.setPage(new Page<>(request, response));
		Page<Tprepayment> page = tprepaymentService.findPage(tprepayment);
		/*for (Tprepayment tar1 : page.getList()) {
			if(tar1.getParentBillType()!=null&&
					!"".equals(tar1.getParentBillType())
					&&tar1.getParentBillCode()!=null&&!"".equals(tar1.getParentBillCode())) {
				String billCodeById = yyService.getBillCodeById(tar1.getParentBillType(), tar1.getParentBillCode());
				tar1.setParentBillCode1(tar1.getParentBillCode());
				tar1.setParentBillCode(billCodeById);
			}
		}*/
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "form")
	public String form(Tprepayment tprepayment, Model model) {

		if(tprepayment.getIsNewRecord()){
			model.addAttribute("rowNumberList", ListUtils.newArrayList());
		} else {
			List<TprepaymentEstimated> rowNumberList =new ArrayList<>();
			for (TprepaymentEstimated tprepaymentEstimated : tprepayment.getTprepaymentEstimatedList()) {
				rowNumberList.add(tprepaymentEstimated);
			}
			model.addAttribute("rowNumberList", rowNumberList);
		}

		//供应商
		List<Supplier> supplierList = supplierService.findList(new Supplier());
		model.addAttribute("supplierList",supplierList);
		//服务订单
		List<ServiceOrder> serviceOrderList = serviceOrderService.findList(new ServiceOrder());
		model.addAttribute("serviceOrderList",serviceOrderList);
		model.addAttribute("tprepayment", tprepayment);
		return "modules/prepay/tprepaymentForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "detail")
	public String detail(Tprepayment tprepayment, Model model) {
		//赋值字段类型名称
		for (TprepaymentEstimated tprepaymentEstimated : tprepayment.getTprepaymentEstimatedList()) {
			tprepaymentEstimated.setPrepaymentTypeName(tprepaymentEstimated.getFixedExpense().getFixedExpenseName());
		}

		for (TprepaymentActual tprepaymentActual : tprepayment.getTprepaymentActualList()) {
			tprepaymentActual.setPrepaymentTypeName(tprepaymentActual.getFixedExpense().getFixedExpenseName());
			tprepaymentActual.setPaymentBankName(tprepaymentActual.getTaccount().getAccountName());
		}
		//行号
		ServiceOrder serviceOrder1 =new ServiceOrder();
		serviceOrder1.setServiceOrderId(tprepayment.getServiceOrderId());
		serviceOrder1=serviceOrderService.get(serviceOrder1);
		List<Long> numberList =new ArrayList<>();
		for (ServiceOrderDetail serviceOrderDetail : serviceOrder1.getServiceOrderDetailList()) {
			numberList.add(serviceOrderDetail.getRowNumber());
		}
		model.addAttribute("numberList",numberList);
        //付款方式
		List<FixedExpense> fixedExpenseList=fixedExpenseService.findList(new FixedExpense());
		model.addAttribute("fixedExpenseList",fixedExpenseList);
		//银行
		Taccount taccount =new Taccount();
		taccount.setIsBank("1");
		List<Taccount> taccountList = taccountService.findList(taccount);
		model.addAttribute("taccountList",taccountList);
		//供应商
		List<Supplier> supplierList = new ArrayList<>();

		//服务订单
		List<ServiceOrder> serviceOrderList = new ArrayList<>();
		Supplier supplier =new Supplier();
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderId(tprepayment.getServiceOrderId());
		supplier.setSupplierId(tprepayment.getSupplierId());
		supplier= supplierService.get(supplier);
		serviceOrder=serviceOrderService.get(serviceOrder);

		serviceOrderList.add(serviceOrder);
		supplierList.add(supplier);
		//如果有前置单据，进行单号查询并赋值
		if(tprepayment.getParentBillType()!=null&&tprepayment.getParentBillCode()!=null&&
		!"".equals(tprepayment.getParentBillType())&&!"".equals(tprepayment.getParentBillCode())) {
			String billCodeById = yyService.getBillCodeById(tprepayment.getParentBillType(), tprepayment.getParentBillCode());
			tprepayment.setParentBillCode1(billCodeById);
		}
		model.addAttribute("supplierList",supplierList);
		model.addAttribute("serviceOrderList",serviceOrderList);
		model.addAttribute("tprepayment", tprepayment);
		return "modules/prepay/tprepaymentDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("prepay:tprepayment:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Tprepayment tprepayment) {

			tprepaymentService.save(tprepayment);

		return renderResult(Global.TRUE, text("保存预付款成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("prepay:tprepayment:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Tprepayment tprepayment) {
		tprepaymentService.delete(tprepayment);
		return renderResult(Global.TRUE, text("删除预付款成功！"));
	}
	/**
	 * 预付单录入
	 */
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "prePayForm")
	public String prePayForm(Tprepayment tprepayment,Model model) {

		model.addAttribute("tprepayment",tprepayment);
		return "modules/prepay/tprepayment/prePayForm";
	}

	//找寻对应的预付款单
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "findPrePay1")
	@ResponseBody
	public List<TprepaymentEstimated> findPrePay1(Tprepayment tprepayment,String serviceorder,String supp) {

		List<TprepaymentEstimated>  list= tprepaymentService.findListBy(supp,serviceorder);

		return list;
	}
	//找寻对应的预付款单
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "findPrePay")
	@ResponseBody
	public List<TprepaymentEstimated> findPrePay(Tprepayment tprepayment,String serviceorder,String supp) {
		ServiceOrder serviceOrder1 =new ServiceOrder();
		serviceOrder1.setServiceOrderCode(serviceorder);
		serviceOrder1 = serviceOrderService.findList(serviceOrder1).get(0);

		List<TprepaymentEstimated>  list= tprepaymentService.findListBy(supp,serviceOrder1.getServiceOrderId());

		return list;
	}
	//找寻对应的预付款单的余额
	@RequiresPermissions("prepay:tprepayment:view")
	@RequestMapping(value = "findLeftMoney")
	@ResponseBody
	public Double findLeftMoney(String value ,String type,Long number) {

		Double d =null;
		Tprepayment tprepayment = new Tprepayment();
		tprepayment.setPrepaymentCode(value);
		tprepayment =tprepaymentService.findList(tprepayment).get(0);
		tprepayment=tprepaymentService.get(tprepayment);

		for (TprepaymentEstimated tprepaymentEstimated : tprepayment.getTprepaymentEstimatedList()) {
			if(tprepaymentEstimated.getRowNumber().equals(number)&&tprepaymentEstimated.getPrepaymentType().equals(type)){
				d= tprepaymentEstimated.getPaymentAmountEstimated();
			}
		}
		return d;
	}


	/**
	 * 财务确认
	 * @param apId
	 * @return
	 */
	@RequestMapping(value = "updateFinancialRecognition")
	@ResponseBody
	public String  updateFinancialRecognition(@RequestParam("apId") String apId, @RequestParam("sta") String sta){
		String str = "";
		int i = tprepaymentService.updateFinancialRecognition(apId,sta);
		if (i > 0){
			str = "操作成功";
		}
		return str;
	}

	//联动查找车架号行号车辆名称
	@RequestMapping(value = "findByMany")
	@ResponseBody
	public ServiceOrderDetail findByMany(@RequestParam("fram") String fram, @RequestParam("ser") String ser, @RequestParam("row") String row){
		return serviceOrderService.findByMany(fram,ser,row);
	}

	//联动查找车架号行号车辆名称
	@RequestMapping(value = "findByMany1")
	@ResponseBody
	public ServiceOrderDetail findByMany1(@RequestParam("fram") String fram, @RequestParam("ser") String ser, @RequestParam("row") String row){
		ServiceOrder serviceOrder =new ServiceOrder();
		serviceOrder.setServiceOrderCode(ser);
		serviceOrder=serviceOrderService.findList(serviceOrder).get(0);

		return serviceOrderService.findByMany(fram,serviceOrder.getServiceOrderId(),row);
	}
}